//
//  YZEModel.m
//  yilutongxing
//
//  Created by 赵继申 on 17/3/11.
//  Copyright © 2017年 YZE. All rights reserved.
//

#import "YZEModel.h"
#import "RPUserInfoBean.h"
#import "MD5.h"
#import "AppDelegate.h"
#import "UserDefaultsTools.h"
#import "CommonDefine.h"
#import "DataUtils.h"

@implementation YZEModel

+(instancetype)sharedInstace{
    static YZEModel *sharedInstace = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        if (sharedInstace == nil) {
            sharedInstace = [[[self class] alloc] init];
            [YZENetwork sharedInstace].isShowLog = NO;
        }
    });
    
    return sharedInstace;
}

//1.登录

-(void)requestLoginWithUserName:(NSString*)userName password:(NSString*)password delegate:(id)delegate requestTag:(int)requestTag{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:userName forKey:@"username"];
    [dic setValue:[MD5 getMd5_32Bit_String:password] forKey:@"password"];
    [dic setObject:APP_CLIENT_TYPE forKey:@"platform"];
    
    YZENetwork *network = [YZENetwork sharedInstace];
    __weak YZENetwork *weakNetwork =network;
    [[YZENetwork sharedInstace] postRequestWithUrl:URL_Login
                                        parameters:dic
                                          delegate:delegate
                                        requestTag:requestTag
                                      successBlock:^(id responseObject)
    {
        [UserDefaultsTools saveToken:responseObject[@"id_token"]];
        NSMutableDictionary *dic = [NSMutableDictionary dictionary];
        [dic setObject:[NSString stringWithFormat:@"Bearer %@",responseObject[@"id_token"]] forKey:@"Authorization"];
        weakNetwork.dicHTTPHeaderField = dic;
        
        if ([delegate respondsToSelector:@selector(OnSuccess:requestTag:)]) {
            [delegate OnSuccess:responseObject requestTag:requestTag];
        }
    }];
}

//获取验证码
-(void)requestGetCaptcha:(NSString *)mobile delegate:(id)delegate requestTag:(int)requestTag{
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setValue:@"string" forKey:@"code"];
    [dic setValue:mobile forKey:@"phone"];
    
    YZENetwork *network = [YZENetwork sharedInstace];
    [network postRequestWithUrl:URL_GetCaptcha parameters:dic delegate:delegate requestTag:requestTag successBlock:^(id responseObject) {
        if ([delegate respondsToSelector:@selector(OnSuccess:requestTag:)]) {
            [delegate OnSuccess:responseObject requestTag:requestTag];
        }
    }];
}

//1.用户信息

-(void)requestUserInfoWithDelegate:(id)delegate requestTag:(int)requestTag{
    //NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    //[dic setValue:[UserDefaultsTools getToken] forKey:@"id_token"];
    
    
    [[YZENetwork sharedInstace] getRequestWithUrl:URL_UserInfo parameters:nil delegate:delegate requestTag:requestTag successBlock:^(id responseObject) {
        [UserDefaultsTools saveUserDefaultsObject:responseObject forKey:USERINFO_KEY];
        if ([delegate respondsToSelector:@selector(OnSuccess:requestTag:)]) {
            [delegate OnSuccess:responseObject requestTag:requestTag];
        }
    }];
}

- (void)requestDistricts{
    [[YZENetwork sharedInstace]getRequestWithUrl:[BASE stringByAppendingString:@"/api/districtsTree"]
                                      parameters:nil
                                        delegate:nil
                                      requestTag:1
                                    successBlock:^(id responseObject)
    {
        NSArray *tree = (NSArray*)responseObject;
        [DataUtils saveDataToPlist:DISTRICTREE_PLISTNAME data:tree];
    }];
}

@end















